package com.greenhouse.business.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.greenhouse.business.domain.BizGroup;
import com.greenhouse.business.service.IBizGroupService;
import com.greenhouse.common.annotation.Log;
import com.greenhouse.common.core.controller.BaseController;
import com.greenhouse.common.core.domain.AjaxResult;
import com.greenhouse.common.core.page.TableDataInfo;
import com.greenhouse.common.enums.BusinessType;
import com.greenhouse.common.utils.poi.ExcelUtil;

/**
 * 大棚管理Controller
 * 
 * @author greenhouse
 * @date 2021-04-12
 */
@RestController
@RequestMapping("/business/group")
public class BizGroupController extends BaseController {
	@Autowired
	private IBizGroupService bizGroupService;

	/**
	 * 查询大棚管理列表
	 */
	@PreAuthorize("@ss.hasPermi('business:group:list')")
	@GetMapping("/list")
	public TableDataInfo list(BizGroup bizGroup) {
		startPage();
		List<BizGroup> list = bizGroupService.selectBizGroupList(bizGroup);
		return getDataTable(list);
	}

	@GetMapping("/getGroupCombo")
	public AjaxResult getGroupCombo() {
		List<BizGroup> list = bizGroupService.selectBizGroupList(new BizGroup());
		JSONArray combo = new JSONArray();
		list.forEach(m -> {
			JSONObject object = new JSONObject();
			object.put("name", m.getGroupName());
			object.put("code", m.getGroupId());
			combo.add(object);
		});

		return AjaxResult.success(combo);
	}

	/**
	 * 导出大棚管理列表
	 */
	@PreAuthorize("@ss.hasPermi('business:group:export')")
	@Log(title = "大棚管理", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(BizGroup bizGroup) {
		List<BizGroup> list = bizGroupService.selectBizGroupList(bizGroup);
		ExcelUtil<BizGroup> util = new ExcelUtil<BizGroup>(BizGroup.class);
		return util.exportExcel(list, "group");
	}

	/**
	 * 获取大棚管理详细信息
	 */
	@PreAuthorize("@ss.hasPermi('business:group:query')")
	@GetMapping(value = "/{groupId}")
	public AjaxResult getInfo(@PathVariable("groupId") Long groupId) {
		return AjaxResult.success(bizGroupService.selectBizGroupById(groupId));
	}

	/**
	 * 新增大棚管理
	 */
	@PreAuthorize("@ss.hasPermi('business:group:add')")
	@Log(title = "大棚管理", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody BizGroup bizGroup) {
		return toAjax(bizGroupService.insertBizGroup(bizGroup));
	}

	/**
	 * 修改大棚管理
	 */
	@PreAuthorize("@ss.hasPermi('business:group:edit')")
	@Log(title = "大棚管理", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody BizGroup bizGroup) {
		return toAjax(bizGroupService.updateBizGroup(bizGroup));
	}

	/**
	 * 删除大棚管理
	 */
	@PreAuthorize("@ss.hasPermi('business:group:remove')")
	@Log(title = "大棚管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{groupIds}")
	public AjaxResult remove(@PathVariable Long[] groupIds) {
		return toAjax(bizGroupService.deleteBizGroupByIds(groupIds));
	}
}
